package com.rfid.library.zhongshan;

import android.text.TextUtils;

import java.util.regex.Pattern;

public class AuthorNo
    {
        /// <summary>
        /// 书次号分类
        /// </summary>
        public String m_ScTy = "-#*+";

        public String m_ZzhMakrSort1 = "=-.(";
        public String m_ZzhMarkSort2 = "-.(";

        /// <summary>
        /// 著者号字符串
        /// </summary>
        public String anStr = "";

        /// <summary>
        /// 著者号字符串长度
        /// </summary>
         int len = 0;

        public AuthorNo(String i_an)
        {
            anStr = i_an;
            len = i_an.length();
        }

       // //#region 检测是否著者号还是种次号
        public boolean Fun_Check(String i_authorNo)
        {
            try
            {
                //第一位是否有括号
                boolean t_IsKh = i_authorNo.substring(0, 1).equals("(");
                if (t_IsKh)
                {
                    //有括号 很有可能是种次号
                    //括号后面再次确认  如果有字母肯定著者号
                    boolean t_BeHStr = Pattern.matches(".*[a-zA-Zア-ンА-Я].*",i_authorNo);
                    if (t_BeHStr)
                    {
                        //含有字母 著者号
                        if (i_authorNo.contains("(#)") || i_authorNo.contains("(*)"))
                        {
                            int t_LIndex = i_authorNo.indexOf("(");
                            int t_RIndex = i_authorNo.indexOf(")");
//                            i_authorNo = i_authorNo.Remove(t_RIndex,1).Remove(t_LIndex,1);
                            i_authorNo = i_authorNo.replace("(", "").replace(")", "");
                        }
                        return false;
                    }
                    else
                    {
                        //种次号
                        return true;
                    }
                }
                else
                {
                    //没有括号
                    //判断是否含有种次号符号"-#*+";
                    String t_strFirst = i_authorNo.substring(0, 1);
                    return m_ScTy.contains(t_strFirst);
                }
            }
            catch(Exception e) { return false; }
        }

        ////#region 比较
        /// <summary>
        /// 按照著者号规则来比较字符大小
        /// </summary>
        /// <param name="c1">      字符1 </param>
        /// <param name="c2">      字符2 </param>
        /// <returns> c1 &lt; c2 -1 <br> c1 == c2 0 <br> c1 &gt; c2 1 </returns>
          int compareChar(char c1, char c2)
        {
            String str2 = "#-.()+=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            int t1 = str2.indexOf(c1);
            int t2 = str2.indexOf(c2);

            if (t1 == -1 && t2 == -1)
            {
//                return c1.CompareTo(c2);return c1.CompareTo(c2);
                return c1 > c2 ? 1 : (c1 ==c2 ? 0 : -1);
            }

            return str2.indexOf(c1) - str2.indexOf(c2);
        }

        /// <summary>
        /// 按种次号  前缀排序
        /// </summary>
        /// <param name="c1">      字符1 </param>
        /// <param name="c2">      字符2 </param>
        /// <returns> c1 &lt; c2 -1 <br> c1 == c2 0 <br> c1 &gt; c2 1 </returns>
        private int compareType(char c1, char c2)
        {
            return m_ScTy.indexOf(c1) - m_ScTy.indexOf(c2);
        }

        /// <summary>
        /// 著者号 纯数字和字母+数字 特殊符号 比较大小
        /// </summary>
        /// <param name="c1"></param>
        /// <param name="c2"></param>
        /// <returns></returns>
        private int compareTypeZzhMark(ZzhType type, String s1, String s2)
        {
            if (type == ZzhType.Number || type == ZzhType.LetAndNumber)
            {
                if (TextUtils.isEmpty(s1))
                {
                    return -1;
                }
                if (TextUtils.isEmpty(s2))
                {
                    return 1;
                }
                return m_ZzhMakrSort1.indexOf(s1) - m_ZzhMakrSort1.indexOf(s2);
            }
            else
            {
                return m_ZzhMarkSort2.indexOf(s1) - m_ZzhMarkSort2.indexOf(s2);
            }
        }

        /// <summary>
        /// 按著作号比较
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
          int compareZZH(String str1, String str2)
        {
            int index = 0, tmp;
            int strLength1 = str1.length(), strLength2 = str2.length();

            while ((strLength1 > index) && (strLength2 > index))
            {
                char c1 = str1.charAt(index);
                char c2 = str2.charAt(index);

                tmp = compareChar(c1, c2);

                if (tmp != 0)
                {
                    return tmp;
                }

                index++;
            }

            if ((strLength1 == index) && (strLength2 == index))
            {
                return 0;
            }

            if ((strLength1 == index) && (strLength2 > index))
            {
                return -1;
            }

            if ((strLength1 > index) && (strLength2 == index))
            {
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 比较吧
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public  int CompareTo(Object o)
        {
            BookIndex.Fun_Message("书次号比较----------->\r\n");
            //外语书当作 种次号来
            String s1 = this.anStr;
            AuthorNo t_an2 = (AuthorNo)o;
            String s2 = t_an2.anStr;

            if (s1.equals(s2))
            {
                //相等
                return 0;
            }

            ////#region 判断两个参数是种次号还是著者号
            boolean t_s1_zc = Fun_Check(s1);
            if (t_s1_zc)
            {
                BookIndex.Fun_Message(s1 + "是种次号\r\n");
            }
            else
            {
                BookIndex.Fun_Message(s1 + "是著者号\r\n");
            }
            boolean t_s2_zc = Fun_Check( s2);
            if (t_s2_zc)
            {
                BookIndex.Fun_Message(s2 + "是种次号\r\n");
            }
            else
            {
                BookIndex.Fun_Message(s2 + "是著者号\r\n");
            }
            ////#endregion

            if (t_s1_zc && t_s2_zc)
            {
                //#region 都是种次号，比较
                //是否前缀带括号
                boolean t_s1_kh = s1.substring(0, 1).equals("(");
                boolean t_s2_kh = s2.substring(0, 1).equals("(");

                //#region 获取种次类型
                char t_s1_zc_type;
                char t_s2_zc_type;

                if (t_s1_kh)
                {
                    //去掉括号
                    s1 = s1.replace("(", "").replace(")", "");
                }
                t_s1_zc_type = s1.charAt(0);

                if (t_s2_kh)
                {
                    s2 = s2.replace("(", "").replace(")", "");
                }
                t_s2_zc_type = s2.charAt(0);

                if (s1.equals(s2))
                {
                    //如果两个种次号相同
                    if ((t_s1_kh && t_s2_kh) || (!t_s1_kh && !t_s2_kh))
                    {
                        return 0;
                    }
                    else if (t_s1_kh && !t_s2_kh)
                    {
                        return -1;
                    }
                    else
                    {
                        return 1;
                    }
                }
                //#endregion

                //#region 种次类型相同
                if (t_s1_zc_type == t_s2_zc_type)
                {
                    //截取
                    String t_zch1 = s1.substring(1);
                    String t_zch2 = s2.substring(1);

                    //种次号头部
                    int t_zch1Head = 0;
                    int t_zch2Head = 0;

                    //种次号尾部
                    int t_zch1Tail = 0;
                    int t_zch2Tail = 0;

                    //#region 解析种次号1
                    try
                    {
                        t_zch1Head = Convert.ToInt16(t_zch1);
                    }
                    catch(Exception e)
                    {
                        //可能有符号
                        int t_mark1Index = t_zch1.indexOf("=");
                        try
                        {
                            t_zch1Head = Convert.ToInt16(t_zch1.substring(0, t_mark1Index));
                        }
                        catch(Exception e1)
                        {
                            BookIndex.Fun_Message("种次号" + t_zch1 + "错误\r\n");
                            return 0;
                        }
                        try
                        {
                            t_zch1Tail = Convert.ToInt16(t_zch1.substring(t_mark1Index + 1));
                        }
                        catch(Exception e2)
                        {
                            BookIndex.Fun_Message("种次号" + t_zch1 + "错误\r\n");
                            return 0;
                        }
                    }
                    //#endregion

                    //#region 解析种次号2
                    try
                    {
                        t_zch2Head = Convert.ToInt16(t_zch2);
                    }
                    catch(Exception e)
                    {
                        //可能有符号
                        int t_mark2Index = t_zch1.indexOf("=");
                        try
                        {
                            t_zch2Head = Convert.ToInt16(t_zch2.substring(0, t_mark2Index));
                        }
                        catch(Exception e1)
                        {
                            BookIndex.Fun_Message("种次号" + t_zch2 + "错误\r\n");
                            return 0;
                        }
                        try
                        {
                            t_zch2Tail = Convert.ToInt16(t_zch2.substring(t_mark2Index + 1));
                        }
                        catch(Exception e2)
                        {
                            BookIndex.Fun_Message("种次号" + t_zch2 + "错误\r\n");
                            return 0;
                        }
                    }
                    //#endregion

                    //#region 比较大小开始
//                    int t_resultHead = t_zch1Head.CompareTo(t_zch2Head);
                    int t_resultHead = t_zch1Head > t_zch2Head ? 1 : (t_zch1Head ==t_zch2Head ? 0 : -1);
                    if (t_resultHead == 0)
                    {
                        //前半段相同就比较后半段 =
//                        return t_zch1Tail.CompareTo(t_zch2Tail);
                        return t_zch1Tail > t_zch2Tail ? 1 : (t_zch1Tail ==t_zch2Tail ? 0 : -1);
                    }
                    else
                    {
                        return t_resultHead;
                    }
                    //#endregion
                }
                //#endregion

                //#region 不同种次类型
                else
                {
                    //按照-#*+排序，一般不会有不同种类种次号比较?
                    return compareType(t_s1_zc_type, t_s2_zc_type);
                }
                //#endregion
                //#endregion
            }
            else if (!t_s1_zc && t_s2_zc)
            {
                //s1 著者号  s2 种次号
                return -1;
            }
            else if (t_s1_zc && !t_s2_zc)
            {
                //s1种次号   s2 著者号
                return 1;
            }
            else
            {
                //#region 都是著者号  比较
                //#region 策略
                //著者号比较头大
                //不能慌，先分解、再分类
                //每个著者号最多分五个部分
                String t_zzh1Part1 = "";
                String t_zzh1Part2 = "";
                String t_zzh1Part3 = "";
                String t_zzh1Part4 = "";
                String t_zzh1Part5 = "";
                ZzhType t_zzh1Type;

                String t_zzh2Part1 = "";
                String t_zzh2Part2 = "";
                String t_zzh2Part3 = "";
                String t_zzh2Part4 = "";
                String t_zzh2Part5 = "";
                ZzhType t_zzh2Type;
                //#endregion

                //#region 分解1
                try
                {

                    Fun_Decompose(".=-()", s1,  t_zzh1Part1,  t_zzh1Part2,  t_zzh1Part3,  t_zzh1Part4,  t_zzh1Part5);
                    boolean t_Is1Letter = Pattern.matches(".*[a-zA-Zア-ンА-Я].*",t_zzh1Part1);
                    if (!t_Is1Letter)
                    {
                        //没有字母那就是  纯数字
                        t_zzh1Type = ZzhType.Number;
                    }
                    else
                    {
                        //可能是字母+数字 或者字母
                        boolean t_Is1Number = Pattern.matches(".*[0-9].*",t_zzh1Part1);
                        if (t_Is1Number)
                        {
                            //有数字和字母
                            t_zzh1Type = ZzhType.LetAndNumber;
                        }
                        else
                        {
                            //只有字母
                            t_zzh1Type = ZzhType.Letter;
                        }
                    }
                }
                catch(Exception e) { return 0; }
                //#endregion

                //#region 分解2
                try
                {
                    Fun_Decompose(".=-()", s2,  t_zzh2Part1,  t_zzh2Part2,  t_zzh2Part3,  t_zzh2Part4,  t_zzh2Part5);
                    boolean t_Is2Letter = Pattern.matches(".*[a-zA-Zア-ンА-Я].*",t_zzh2Part1);
                    if (!t_Is2Letter)
                    {
                        //没有字母那就是  纯数字
                        t_zzh2Type = ZzhType.Number;
                    }
                    else
                    {
                        //可能是字母+数字 或者字母
                        boolean t_Is2Number = Pattern.matches( ".*[0-9].*",t_zzh2Part1);
                        if (t_Is2Number)
                        {
                            //有数字和字母
                            t_zzh2Type = ZzhType.LetAndNumber;
                        }
                        else
                        {
                            //只有字母
                            t_zzh2Type = ZzhType.Letter;
                        }
                    }
                }
                catch (Exception e){ return 0; }
                //#endregion

                //#region 比较大小
                int t_result=0;
                //#region 都是数字类型
                if (t_zzh1Type == ZzhType.Number && t_zzh2Type == ZzhType.Number)
                {
                    //#region 都是数字类型
                    try
                    {
                        int t_all1N1 = Convert.ToInt16(t_zzh1Part1);
                        int t_all2N1 = Convert.ToInt16(t_zzh2Part1);
//                        t_result = t_all1N1.CompareTo(t_all2N1);
                        t_result = t_all1N1 > t_all2N1 ? 1 : (t_all1N1 ==t_all2N1 ? 0 : -1);
                        if (t_result == 0)
                        {
                            //比较符号
                            if (TextUtils.isEmpty(t_zzh1Part2)&& TextUtils.isEmpty(t_zzh2Part2)) { return 0; }
                            t_result = compareTypeZzhMark(ZzhType.Number, t_zzh1Part2, t_zzh2Part2);
                            if (t_result == 0)
                            {
                                if (TextUtils.isEmpty(t_zzh1Part3)) { t_zzh1Part3 = "0"; }
                                if (TextUtils.isEmpty(t_zzh2Part3)) { t_zzh2Part3 = "0"; }
                                int t_all1N2 = Convert.ToInt16(t_zzh1Part3);
                                int t_all2N2 = Convert.ToInt16(t_zzh2Part3);
//                                t_result = t_all1N2.CompareTo(t_all2N2);
                                t_result = t_all1N2 > t_all2N2 ? 1 : (t_all1N2 ==t_all2N2 ? 0 : -1);
                                if (t_result == 0)
                                {
                                    //比较符号
                                    if (TextUtils.isEmpty(t_zzh1Part4) && TextUtils.isEmpty(t_zzh2Part4)) { return 0; }
                                    t_result = compareTypeZzhMark(ZzhType.Number, t_zzh1Part4, t_zzh2Part4);
                                    if (t_result == 0)
                                    {
                                        if (TextUtils.isEmpty(t_zzh1Part5)) { t_zzh1Part5 = "0"; }
                                        if (TextUtils.isEmpty(t_zzh2Part5)) { t_zzh2Part5 = "0"; }
                                        int t_all1N3 = Convert.ToInt16(t_zzh1Part5);
                                        int t_all2N3 = Convert.ToInt16(t_zzh2Part5);
//                                        t_result = t_all1N3.CompareTo(t_all2N3);
                                        t_result = t_all1N3 > t_all2N3 ? 1 : (t_all1N3 ==t_all2N3 ? 0 : -1);
                                        if (t_result == 0)
                                        {
                                            return 0;
                                        }
                                        else
                                        {
                                            return t_result;
                                        }
                                    }
                                    else
                                    {
                                        return t_result;
                                    }
                                }
                                else
                                {
                                    return t_result;
                                }
                            }
                            else
                            {
                                return t_result;
                            }
                        }
                        else
                        {
                            return t_result;
                        }
                    }
                    catch (Exception ce) { 
                        BookIndex.Fun_Message("格式不正确：" + ce.getMessage() + "\r\n"); return 0; 
                }}
                //#endregion

                //#region 都是字母类型
                else if (t_zzh1Type == ZzhType.Letter && t_zzh2Type == ZzhType.Letter)
                {
                    try
                    {
                        t_result = compareZZH(t_zzh1Part1,t_zzh2Part1);
                        if (t_result == 0)
                        {
                            if (TextUtils.isEmpty(t_zzh1Part2) && TextUtils.isEmpty(t_zzh2Part2)) { return 0; }
                            t_result = compareTypeZzhMark(ZzhType.Letter,t_zzh1Part2,t_zzh2Part2);
                            if (t_result == 0)
                            {
                                if (TextUtils.isEmpty(t_zzh1Part3)&& TextUtils.isEmpty(t_zzh2Part3)) { return 0; }
                                t_result = compareZZH(t_zzh1Part3, t_zzh2Part3);
                                if (t_result == 0)
                                {
                                    if (TextUtils.isEmpty(t_zzh1Part4) && TextUtils.isEmpty(t_zzh2Part4)) { return 0; }
                                    t_result = compareTypeZzhMark(ZzhType.Letter, t_zzh1Part4, t_zzh2Part4);
                                    if (t_result == 0)
                                    {
                                        if (TextUtils.isEmpty(t_zzh1Part5) && TextUtils.isEmpty(t_zzh2Part5)) { return 0; }
                                        t_result = compareZZH(t_zzh1Part5, t_zzh2Part5);
                                    }
                                    else
                                    {
                                        return t_result;
                                    }
                                }
                                else
                                {
                                    return t_result;
                                }
                            }
                            else
                            {
                                return t_result;
                            }
                        }
                        else
                        {
                            return t_result;
                        }
                    }
                    catch (Exception ce) { 
                        BookIndex.Fun_Message("格式不正确：" + ce.getMessage() + "\r\n"); return 0; 
                }}
                //#endregion

                //#region 都是字母数字类型
                else if (t_zzh1Type == ZzhType.LetAndNumber && t_zzh2Type == ZzhType.LetAndNumber)
                {
                    try
                    {
                        t_result = compareZZH(t_zzh1Part1, t_zzh2Part1);
                        if (t_result == 0)
                        {
                            if (TextUtils.isEmpty(t_zzh1Part2) && TextUtils.isEmpty(t_zzh2Part2)) { return 0; }
                            t_result = compareTypeZzhMark(ZzhType.Number, t_zzh1Part2, t_zzh2Part2);
                            if (t_result == 0)
                            {
                                if (TextUtils.isEmpty(t_zzh1Part3) && TextUtils.isEmpty(t_zzh2Part3)) { return 0; }
                                t_result = compareZZH(t_zzh1Part3, t_zzh2Part3);
                                if (t_result == 0)
                                {
                                    if (TextUtils.isEmpty(t_zzh1Part4)&& TextUtils.isEmpty(t_zzh2Part4)) { return 0; }
                                    t_result = compareTypeZzhMark(ZzhType.Number, t_zzh1Part4, t_zzh2Part4);
                                    if (t_result == 0)
                                    {
                                        if (TextUtils.isEmpty(t_zzh1Part5) && TextUtils.isEmpty(t_zzh2Part5)) { return 0; }
                                        t_result = compareZZH(t_zzh1Part5, t_zzh2Part5);
                                    }
                                    else
                                    {
                                        return t_result;
                                    }
                                }
                                else
                                {
                                    return t_result;
                                }
                            }
                            else
                            {
                                return t_result;
                            }
                        }
                        else
                        {
                            return t_result;
                        }
                    }
                    catch (Exception ce) {
                        BookIndex.Fun_Message("格式不正确：" + ce.getMessage() + "\r\n"); return 0; 
                }}
                //#endregion

                //#region 数字和字母
                else if (t_zzh1Type == ZzhType.Number && t_zzh2Type == ZzhType.Letter)
                {
                    //直接排序
                    return -1;
                }
                //#endregion

                //#region 数字和字母数字
                else if (t_zzh1Type == ZzhType.Number && t_zzh2Type == ZzhType.LetAndNumber)
                {
                    //直接排序
                    return -1;
                }
                //#endregion

                //#region 字母和数字
                else if (t_zzh1Type == ZzhType.Letter && t_zzh2Type == ZzhType.Number)
                {
                    //直接排序
                    return 1;
                }
                //#endregion

                //#region 字母数字和数字
                else if (t_zzh1Type == ZzhType.LetAndNumber && t_zzh2Type == ZzhType.Number)
                {
                    //直接排序
                    return 1;
                }
                //#endregion

                //#region 字母和字母数字 或者 字母数字和字母
                else
                {
                    //只要比较前段就可以判断出结果
                    return compareZZH(t_zzh1Part1, t_zzh2Part1);
                }
                //#endregion
                //#endregion

                //#endregion
            }

            return 0;
        }

        /// <summary>
        /// 分解著者号
        /// </summary>
        private void Fun_Decompose(String i_mark,String i_zzh, 
             String i_zzhPart1, 
             String i_zzhPart2, 
             String i_zzhPart3,
             String i_zzhPart4, 
             String i_zzhPart5)
        {
            String t_zzh=i_zzh.replace(")","");
            int t_PartLenth = 0;
            int t_PreIndex = 0;
            for (int i = 0; i < t_zzh.length(); i++)
            {
                if (i_mark.contains(t_zzh.charAt(i) + ""))
                {
                    int t_markIndex = i;
                    String t_str = "";
                    if (t_PreIndex == 0)
                    {
                        t_str = t_zzh.substring(0, t_markIndex);
                    }
                    else
                    {
                        t_str = t_zzh.substring(t_PreIndex + 1, t_markIndex - t_PreIndex - 1);
                    }
                    if (TextUtils.isEmpty(t_str))
                    {
                        t_str = t_zzh.charAt(i) + "";
                    }
                     
                    switch (t_PartLenth)
                    {
                        case 0:
                            i_zzhPart1 = t_str;
                            i_zzhPart2 = t_zzh.charAt(i) + "";
                            break;
                        case 1:
                            i_zzhPart3 = t_str;
                            i_zzhPart4 = t_zzh.charAt(i) + "";
                            break;
                        case 2:
                            i_zzhPart5 = t_str;
                            break;
                        default:
                            break;
                    }
                    t_PreIndex = i;
                    t_PartLenth++;
                }
            }
            if (t_PreIndex < t_zzh.length())
            {
                //剩余字符
                if (t_PreIndex == 0)
                {
                    i_zzhPart1 = t_zzh;
                }
                else
                {
                    String t_str = t_zzh.substring(t_PreIndex + 1);
                    switch (t_PartLenth)
                    {
                        case 1:
                            i_zzhPart3= t_str;
                            break;
                        case 2:
                            i_zzhPart5= t_str;
                            break;
                        default:
                            break;
                    }
                }
            }

//            BookIndex.Fun_Message(i_zzh.PadRight(10, ' ') + "分解：一>> " + i_zzhPart1.PadRight(6, ' ')
//                + "二>> " + i_zzhPart2.PadRight(6, ' ')
//                + "三>> " + i_zzhPart3.PadRight(6, ' ')
//                + "四>> " + i_zzhPart4.PadRight(6, ' ')
//                + "五>> " + i_zzhPart5 + "\r\n");
        }
        //#endregion
    }